<template>
  <div class="auction-list-container">
    <!-- 搜索表单 -->
    <div class="search-form">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="拍卖品名称" prop="title">
          <el-input v-model="searchForm.title" placeholder="请输入拍卖品名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格 -->
    <el-table :data="tableData" border style="width: 100%" v-loading="loading">
      <el-table-column type="index" label="序号" width="80" :index="1" align="center"></el-table-column>
      <el-table-column prop="title" label="拍卖品名称"></el-table-column>
      <el-table-column prop="startPrice" label="起拍价">
        <template #default="scope">
          ¥{{ scope.row.startPrice }}
        </template>
      </el-table-column>
      <el-table-column prop="startTime" label="开始时间"></el-table-column>
      <el-table-column prop="endTime" label="结束时间"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ getStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button size="small" type="primary" @click="viewDetail(scope.row.id)">查看</el-button>
          <el-button size="small" type="success" @click="reviewAuction(scope.row)"
            v-if="scope.row.status === 'PENDING'">审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="searchForm.page" :page-sizes="[10, 20, 50, 100]" :page-size="searchForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 审核对话框 -->
    <el-dialog title="审核拍卖品" v-model="reviewDialogVisible" width="500px">
      <p>拍卖品名称: {{ currentAuction.title }}</p>
      <p>当前状态: {{ getStatusText(currentAuction.status) }}</p>
      <div>
        <p>请选择审核结果:</p>
        <el-radio-group v-model="reviewStatus">
          <el-radio label="ACTIVE">通过</el-radio>
          <el-radio label="CANCELLED">拒绝</el-radio>
        </el-radio-group>
      </div>
      <template #footer>
        <el-button @click="reviewDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmReview">确认</el-button>
      </template>
    </el-dialog>

    <!-- 拍卖品详情对话框 -->
    <el-dialog title="拍卖品详情" v-model="detailDialogVisible" width="800px">
      <div v-if="auctionDetail" class="auction-detail">
        <el-descriptions :column="2" border>

          <el-descriptions-item label="名称">{{ auctionDetail.title }}</el-descriptions-item>
          <el-descriptions-item label="卖家">{{ auctionDetail.sellerName }}</el-descriptions-item>
          <el-descriptions-item label="分类">{{ auctionDetail.categoryName }}</el-descriptions-item>
          <el-descriptions-item label="起拍价">¥{{ auctionDetail.startPrice }}</el-descriptions-item>
          <el-descriptions-item label="开始时间">{{ auctionDetail.startTime }}</el-descriptions-item>
          <el-descriptions-item label="结束时间">{{ auctionDetail.endTime }}</el-descriptions-item>
          <el-descriptions-item label="状态">
            <el-tag :type="getStatusType(auctionDetail.status)">{{ getStatusText(auctionDetail.status) }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="竞价次数">{{ auctionDetail.bidCount || 0 }}</el-descriptions-item>
        </el-descriptions>

        <div class="detail-section">
          <h4>商品图片</h4>
          <div class="image-container">
            <el-image :src="auctionDetail.image" fit="contain" :preview-src-list="[auctionDetail.image]" />
          </div>
        </div>

        <div class="detail-section">
          <h4>商品描述</h4>
          <p>{{ auctionDetail.description }}</p>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { getAuctionList, getAuctionDetailAdmin, reviewAuction as reviewAuctionApi } from '@/api/auction'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const searchForm = reactive({
  title: '',
  page: 1,
  pageSize: 10
})
const tableData = ref([])
const total = ref(0)
const loading = ref(false)
const router = useRouter()

// 审核相关
const reviewDialogVisible = ref(false)
const reviewStatus = ref('ACTIVE')
const currentAuction = ref({})

const getStatusType = (status) => {
  const statusMap = {
    'PENDING': 'warning',
    'ACTIVE': 'success',
    'FINISHED': 'info',
    'CANCELLED': 'danger'
  }
  return statusMap[status] || 'info'
}

const getStatusText = (status) => {
  const statusMap = {
    'PENDING': '待审核',
    'ACTIVE': '已激活',
    'FINISHED': '已结束',
    'CANCELLED': '已取消'
  }
  return statusMap[status] || '未知状态'
}

const loadAuctionList = async () => {
  loading.value = true
  try {
    const res = await getAuctionList(searchForm)
    console.log('拍卖品列表响应:', res)

    if (res.code === 1 && res.data) {
      tableData.value = res.data.records || []
      total.value = res.data.total || 0

      if (tableData.value.length === 0) {
        ElMessage.warning('暂无数据')
      }
    } else {
      tableData.value = []
      total.value = 0
      ElMessage.warning(res.msg || '获取数据失败')
    }
  } catch (error) {
    console.error('加载拍卖品列表失败:', error)
    ElMessage.error('加载拍卖品列表失败')
    tableData.value = []
    total.value = 0
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  searchForm.page = 1
  loadAuctionList()
}

const resetSearch = () => {
  searchForm.title = ''
  searchForm.page = 1
  loadAuctionList()
}

const handleSizeChange = (val) => {
  searchForm.pageSize = val
  loadAuctionList()
}

const handleCurrentChange = (val) => {
  searchForm.page = val
  loadAuctionList()
}

const auctionDetail = ref(null)
const detailDialogVisible = ref(false)

const viewDetail = async (id) => {
  try {
    loading.value = true
    const res = await getAuctionDetailAdmin(id)
    if (res) {
      auctionDetail.value = res
      detailDialogVisible.value = true
    } else {
      ElMessage.error('获取拍卖品详情失败')
    }
  } catch (error) {
    console.error('获取拍卖品详情失败:', error)
    ElMessage.error('获取拍卖品详情失败：' + error.message)
  } finally {
    loading.value = false
  }
}

const reviewAuction = (auction) => {
  currentAuction.value = auction
  reviewStatus.value = 'ACTIVE'
  reviewDialogVisible.value = true
}

const confirmReview = async () => {
  try {
    loading.value = true
    const res = await reviewAuctionApi(currentAuction.value.id, reviewStatus.value)
    if (res.code === 1) {
      ElMessage.success('审核成功')
      reviewDialogVisible.value = false
      loadAuctionList()
    } else {
      ElMessage.error(res.msg || '审核失败')
    }
  } catch (error) {
    console.error('审核失败:', error)
    ElMessage.error('审核失败')
  } finally {
    loading.value = false
  }
}

onMounted(loadAuctionList)
</script>

<style scoped>
.auction-list-container {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.auction-list-container {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

/* 拍卖品详情样式 */
.auction-detail .detail-section {
  margin-top: 20px;
}

.auction-detail .detail-section h4 {
  margin-bottom: 10px;
  font-size: 16px;
  color: #333;
}

.auction-detail .image-container {
  display: flex;
  justify-content: center;
  margin: 10px 0;
}

.auction-detail .el-image {
  max-width: 100%;
  max-height: 400px;
  border-radius: 4px;
}

.auction-detail p {
  margin: 0;
  line-height: 1.6;
  color: #666;
}
</style>